package dao;

import dbutil.DBHelp;
import vo.Course;
import vo.Lec;
import vo.User;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by ssw on 2018/6/12.
 */
public class UsersDao {

    public User login(String uid, String upass) {
        User users=null;
        String sql="select * from users where uid=? and upass =?";
        DBHelp hp=new DBHelp();
        try {
            ResultSet rs=hp.query(sql,uid,upass);
            if(rs.next()){
                users=new User(rs.getString(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getInt(5),rs.getString(6),rs.getString(7),rs.getString(8),rs.getInt(9));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                hp.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return users;
    }

    public boolean addLec(String lid, String lpass, String lname) {
        int temp=-1;
        String sql="insert into users(uid,upass,uname) values(?,?,?)";
        String sql2="insert into lec(lid) values(?)";
        DBHelp hp=new DBHelp();
        try {
            temp=hp.insert(sql,lid,lpass,lname);
            temp=hp.insert(sql2);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                hp.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return temp>0;
    }

    public boolean addUser(String sid, String upass, String sname, String clname) {
        int temp=-1;
        String sql="insert into users(uid,upass,uname) values(?,?,?)";
        String sql2="insert into students(sid,clname) values(?,?)";
        DBHelp hp=new DBHelp();
        try {
            temp=hp.insert(sql,sid,upass,sname);
            temp=hp.insert(sql2,sid,clname);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                hp.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return temp>0;
    }

    public List<Lec> askLec() {
        DBHelp dh=new DBHelp();
        List<Lec> lecList=new LinkedList<>();
        String sql="select * from users";
        try {
            ResultSet rs=dh.query(sql);
            while(rs.next()){
                if(rs.getString(1).startsWith("lec"))
                    lecList.add(new Lec(rs.getString(1),rs.getString(4)));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                dh.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return lecList;
    }

    public User askStuInfo(String uid) {
        DBHelp dh=new DBHelp();
        User userInfo=new User();
        String sql="select * from users where uid=?";
        try {
            ResultSet rs=dh.query(sql,uid);
            if(rs.next()){
                    userInfo.setUid(rs.getString(1));
                    userInfo.setUpass(rs.getString(2));
                    userInfo.setUname(rs.getString(4));
                    userInfo.setUage(Integer.parseInt(rs.getString(5)));
                    userInfo.setUsex(rs.getString(6));
                    userInfo.setUtext(rs.getString(8));

            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                dh.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return userInfo;
    }
}
